{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Introducing `ApiChain`! 🚀 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from langchain.chains import APIChain\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "from IPython.display import Markdown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set environment variables\n",
    "os.environ['OPENAI_API_KEY'] = '...'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_message = 'Whats is the weather like in New Delhi today?'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "The weather in New Delhi today is moderate to heavy rain with thunder, with a temperature of 25 degrees Celsius (77 degrees Fahrenheit) and high humidity."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "llm = ChatOpenAI(temperature=0, model_name=\"gpt-3.5-turbo\", max_tokens=256, verbose=True)\n",
    "\n",
    "apiSpec = \"\"\"API documentation:\n",
    "Base URL:f\"http://api.weatherapi.com/\n",
    "Endpoint: /weather\n",
    "Example API call: http://api.weatherapi.com/v1/current.json?\\\n",
    "key=**your_api_key**&q=Delhi&aqi=no'\n",
    "\n",
    "This API is for retrieving weather information based on city name\n",
    "\n",
    "Request GET \n",
    "Query Parameter Name\tFormat\tRequired\tDescription\n",
    "q\tString\tYes\tName of city for which we want weather info\n",
    "\n",
    "INSTRUCTIONS FOR RESPONDING\n",
    "Respond in Natural Language\n",
    "\"\"\"\n",
    "\n",
    "chain = APIChain.from_llm_and_api_docs(llm, apiSpec, verbose=False)\n",
    "response = chain.run(user_message)\n",
    "Markdown(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_twitter",
   "language": "python",
   "name": "env_twitter"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
